
/* GCSx
** MOUSE.CPP
**
** Basic mouse display
*/

/*****************************************************************************
** Copyright (C) 2003-2006 Janson
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
** 
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
*****************************************************************************/

#include "all.h"

MousePointer::MousePointer(const char* filename, int x, int y) { start_func
    assert(filename);
    assert(x >= 0);
    assert(y >= 0);

    original = SDL_LoadBMP(filename);
    if (original == NULL) {
        fatalCrash(0, "Error loading mouse cursor %s: %s", filename, SDL_GetError());
    }
    SDL_SetColorKey(original, SDL_SRCCOLORKEY, SDL_MapRGB(original->format, 255, 0, 0));
    converted = NULL;
    hotX = x;
    hotY = y;
    
    tex = NULL;
    texNum = 0;
}

MousePointer::~MousePointer() { start_func
    if (original) SDL_FreeSurface(original);
    if (converted) SDL_FreeSurface(converted);
}

int MousePointer::xSize() const { start_func
    assert(original);
    return original->w;
}
    
int MousePointer::ySize() const { start_func
    assert(original);
    return original->h;
}
    
int MousePointer::tooltipYOffset() const { start_func
    assert(original);
    return original->h - hotY;
}

void MousePointer::convertFor(SDL_Surface* screen) { start_func
    assert(screen);

    if (converted) SDL_FreeSurface(converted);
    converted = NULL;
    converted = SDL_ConvertSurface(original, screen->format, screen->flags);
    tex = NULL;
}

void MousePointer::convertGL(TextureMap* texmap, int gNum, int xSize, int ySize) { start_func
    assert(texmap);

    SDL_Surface* temp = createSurface32(xSize, ySize);
    SDL_Rect sourceRect;
    sourceRect.x = 0;
    sourceRect.y = 0;
    sourceRect.w = original->w;
    sourceRect.h = original->h;
    SDL_Rect destRect = sourceRect;
    if (SDL_BlitSurface(original, &sourceRect, temp, &destRect)) {
        fatalCrash(0, "SDL Blit error: %s", SDL_GetError());
    }
    texmap->store(gNum, temp);
    SDL_FreeSurface(temp);
    
    tex = texmap;
    texNum = gNum;
}

void MousePointer::textureAt(int x, int y) const { start_func
    if (tex) tex->draw(texNum, x - hotX, y - hotY);
}

void MousePointer::displayAt(int x, int y, SDL_Surface* save, SDL_Rect* storeRect) { start_func
    assert((converted) || (original));
    if (save) blit(x - hotX, y - hotY, getScreen(), 0, 0, save, save->w, save->h);

    SDL_Rect sourceRect;
    sourceRect.x = 0;
    sourceRect.y = 0;

    storeRect->x = x - hotX;
    storeRect->y = y - hotY;

    if (converted) {
        sourceRect.w = converted->w;
        sourceRect.h = converted->h;
        if (SDL_BlitSurface(converted, &sourceRect, getScreen(), storeRect)) {
            fatalCrash(0, "SDL Blit error: %s", SDL_GetError());
        }
    }
    else {
        sourceRect.w = original->w;
        sourceRect.h = original->h;
        if (SDL_BlitSurface(original, &sourceRect, getScreen(), storeRect)) {
            fatalCrash(0, "SDL Blit error: %s", SDL_GetError());
        }
    }
}

void MousePointer::unDisplay(int x, int y, SDL_Surface* saved, SDL_Rect* storeRect) const { start_func
    if (saved) {
        SDL_Rect sourceRect;
        sourceRect.x = 0;
        sourceRect.y = 0;
        sourceRect.w = saved->w;
        sourceRect.h = saved->h;
        
        storeRect->x = x - hotX;
        storeRect->y = y - hotY;
        
        if (SDL_BlitSurface(saved, &sourceRect, getScreen(), storeRect)) {
            fatalCrash(0, "SDL Blit error: %s", SDL_GetError());
        }
    }
}

